package com.study.bigdata.spark.core.summer.service

import com.study.bigdata.spark.core.summer.common.TService
import com.study.bigdata.spark.core.summer.dao.HotCategeryTop10Dao
import com.study.bigdata.spark.core.summer.dao.HotCategeryTop10Dao

class HotCategeryTop10Service extends TService{
  private val hotCategeryTop10Dao  = new HotCategeryTop10Dao

  override def analysis() = {
    val fileDatas = hotCategeryTop10Dao.readFileBySpark("data/user_visit_action.txt")
    val flatDatas = fileDatas.flatMap(
      data => {
        val datas = data.split("_")
        if (datas(6) != "-1") {
          List((datas(6), (1, 0, 0)))
        } else if (datas(8) != "null") {
          val id = datas(8)
          val ids = id.split(",")
          ids.map(
            id => {
              (id, (0, 1, 0))
            }
          )

        } else if (datas(10) != "null") {
          val id = datas(10)
          val ids = id.split(",")
          ids.map(
            id => {
              (id, (0, 0, 1))
            }
          )
        } else {
          Nil
        }
      }
    )
    val top10 = flatDatas.reduceByKey(
      (t1, t2) => {
        (t1._1 + t2._1, t1._2 + t2._2, t1._3 + t2._3)
      }
    ).sortBy(_._2, false).take(10)
    top10
  }
}
